package control
{
	import flash.events.Event;
	
	import model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import vo.User;
	public class loginControl
	{
	    internal var theModel:ModelLocator = ModelLocator.getInstance();
		
		private var loginRO:RemoteObject;
		public function loginControl()
		{
			loginRO = new RemoteObject();
			loginRO.destination="login";
			
			loginRO.doLogin.addEventListener(ResultEvent.RESULT,loginResultHandler);
			loginRO.addEventListener(FaultEvent.FAULT, loginFaultHandler);
		}
		
		public function doLogin(username:String,password:String):void{
			loginRO.doLogin(username,password);        
		}
		private function httpEncoding(param:String):String{
                return encodeURIComponent(param);
        }

		internal function loginResultHandler(event:ResultEvent):void {
         		//
         		var obj:Object=event.result as Object;
         		if(event.result!= null){
         			//Alert.show("here");
         			theModel.user=new User();
         			theModel.user.fill(obj);
         			theModel.dispatchEvent(new Event(ModelLocator.LOGIN_OK));
         		}else{
         			theModel.dispatchEvent(new Event(ModelLocator.LOGIN_FAILED));
         		}
        }
		
		internal function loginFaultHandler (event:FaultEvent):void {
         		Alert.show(event.fault.message,"Error")
        }

	}
}